> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-wb_mcp_and_llms_rewrite.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Op

> The Op decorator and related functionality for tracking function calls

The `@weave.op` decorator is used to track function calls in Weave. It automatically logs inputs, outputs, and metadata for decorated functions.

## Basic Usage

```python
import weave

@weave.op
def my_function(x: int, y: int) -> int:
    return x + y

# When called, this function is automatically tracked
result = my_function(3, 4)
```

## Decorator Options

### name

Set a custom name for the operation.

```python
@weave.op(name="custom_operation_name")
def my_function():
    pass
```

### call\_display\_name

Set a display name for all calls of this operation.

```python
@weave.op(call_display_name="My Custom Display")
def my_function():
    pass
```

You can also use a function to dynamically generate display names:

```python
def generate_name(call):
    return f"Processing {call.inputs['item_id']}"

@weave.op(call_display_name=generate_name)
def process_item(item_id: str):
    pass
```

### postprocess\_inputs

Transform inputs before they are logged.

```python
def redact_sensitive(inputs):
    if 'password' in inputs:
        inputs['password'] = '[REDACTED]'
    return inputs

@weave.op(postprocess_inputs=redact_sensitive)
def login(username: str, password: str):
    pass
```

### postprocess\_output

Transform outputs before they are logged.

```python
def format_output(output):
    return weave.Markdown(f"**Result:** {output}")

@weave.op(postprocess_output=format_output)
def generate_report():
    return "Analysis complete"
```

## Op Methods

### call

Get both the result and the Call object.

```python
@weave.op
def my_function(x: int) -> int:
    return x * 2

result, call = my_function.call(5)
print(f"Result: {result}")
print(f"Call ID: {call.id}")
```

### name

Get or set the operation name.

```python
@weave.op
def my_function():
    pass

# Get the current name
print(my_function.name)

# Set a new name
my_function.name = "new_operation_name"
```

## Working with Async Functions

Weave supports async operations:

```python
@weave.op
async def async_operation(prompt: str) -> str:
    # Async operations are tracked the same way
    result = await some_async_api(prompt)
    return result
```

## Working with Generators

Weave can track generator functions:

```python
@weave.op
def generate_items(count: int):
    for i in range(count):
        yield f"Item {i}"

# The output is captured when the generator is consumed
items = list(generate_items(5))
```

## Class Methods

Decorate methods on classes:

```python
class MyClass:
    @weave.op
    def process(self, data: str) -> str:
        return data.upper()

instance = MyClass()
result = instance.process("hello")
```

## Static and Class Methods

```python
class MyClass:
    @staticmethod
    @weave.op
    def static_method(x: int) -> int:
        return x * 2
    
    @classmethod
    @weave.op
    def class_method(cls, x: int) -> int:
        return x * 3
```

<Note>
  The `@weave.op` decorator should be the innermost decorator when stacking decorators.
</Note>
